package org.wlw.servlet;

import org.wlw.factoy.SimpleFactory;
import org.wlw.models.Movie;
import org.wlw.service.IMovieService;
import org.wlw.utils.Contrants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/movie/update")
public class MovieUpdateServlet extends HttpServlet {

    private IMovieService movieService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        movieService = SimpleFactory.getMovieService();
        int id = Integer.parseInt(req.getParameter("id"));
        Movie movie = movieService.load(id);
        req.setAttribute("movie",movie);
        req.getRequestDispatcher(Contrants.JSP_PREFIX+"/movie/update.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        movieService = SimpleFactory.getMovieService();
        Movie tm = movieService.load(id);
        tm.setType(req.getParameter("type"));
        tm.setName(req.getParameter("name"));
        tm.setScore(Double.parseDouble(req.getParameter("score")));
        tm.setDirector(req.getParameter("director"));
        tm.setActor(req.getParameter("actor"));
        movieService.update(tm);
        resp.sendRedirect(req.getContextPath()+"/movies");
    }
}
